import {
  type DoctorPage,
  type FollowType,
  type KnowledgePage,
  type KnowledgeParams,
  type PageParams,
  type TopDep,
  type Image,
  type ConsultOrderPreParams,
  type ConsultOrderPreData,
  type PartialConsult,
  type PayParams,
  type ConsultOrderItem,
  type ConsultOrderListParams,
  type ConsultOrderPage
} from './../types/consult.d'
import { request } from './../utils/request'

/**
 * 获取首页文章列表
 * @param params
 */
export const getKnowledgePage = (params: KnowledgeParams) =>
  request<KnowledgePage>('/patient/home/knowledge', 'GET', params)

/**
 * 获取首页推荐医生列表
 * @param params
 */
export const getDoctorList = (params: PageParams) =>
  request<DoctorPage>('/home/page/doc', 'GET', params)

/**
 * 关注与取消关注
 * @param id 对应类别项的id
 * @param type 关注的类型
 */
export const followOrUnfollow = (id: string, type: FollowType = 'doc') =>
  request<{ id: string }>('/like', 'POST', { id, type })

/**
 * 获取所有科室
 */
export const getAllDep = () => request<TopDep[]>('/dep/all')

/**
 * 上传图片
 * @param file 图片对象
 */
export const uploadImage = (file: File) => {
  // 1. 创建一个formData对象
  const fd = new FormData()
  fd.append('file', file)

  // 2. 实现接口上传
  return request<Image>('/upload', 'POST', fd)
}

/**
 * 获取问诊订单预支付信息
 * @param params 预支付参数对象
 */
export const getConsultOrderPre = (params: ConsultOrderPreParams) =>
  request<ConsultOrderPreData>('/patient/consult/order/pre', 'GET', params)

/**
 * 生成订单id
 * @param data 收集的问诊信息
 */
export const createConsultOrder = (data: PartialConsult) =>
  request<{ id: string }>('/patient/consult/order', 'POST', data)

/**
 * 根据信息获取支付的跳转地址
 * @param data
 */
export const getConsultOrderPayUrl = (data: PayParams) =>
  request<{ payUrl: string }>('/patient/consult/pay', 'POST', data)

/**
 * 获取问诊订单详情
 * @param orderId 问诊订单
 */
export const getConsultOrderDetail = (orderId: string) =>
  request<ConsultOrderItem>('/patient/consult/order/detail', 'GET', { orderId })

/**
 * 查看处方
 * @param id 处方id
 */
export const getPrescriptionPic = (id: string) =>
  request<Image>(`/patient/consult/prescription/${id}`)

/**
 * 评价问诊
 * @param data 评价的数据对象
 */
export const evaluateConsultOrder = (data: {
  docId: string
  orderId: string
  score: number
  content: string
  anonymousFlag: 0 | 1
}) => request<{ id: string }>('/patient/order/evaluate', 'POST', data)

/**
 * 获取我的问诊订单
 * @param params 获取订单的参数对象
 */
export const getConsultOrderList = (params: ConsultOrderListParams) =>
  request<ConsultOrderPage>('/patient/consult/order/list', 'GET', params)

/**
 * 取消订单
 * @param id 订单的id
 */
export const cancelOrder = (id: string) =>
  request(`/patient/order/cancel/${id}`, 'PUT')

/**
 * 删除订单
 * @param id 订单的id
 */
export const deleteOrder = (id: string) =>
  request(`/patient/order/${id}`, 'DELETE')
